#include "BreakPointStateSST.h"

BreakPointStateSST::BreakPointStateSST()
:status(END)
{
    static SSTType staSST = {
        {0, 1, 0, 0, 0,},
        {1, 0, 1, 0, 0,},
        {1, 0, 0, 1, 1,},
        {1, 0, 0, 0, 1,},
        {1, 0, 0, 0, 0,},
    };
    sst = &staSST;
}

BreakPointStateSST::~BreakPointStateSST(){
    sst = nullptr;
}

bool BreakPointStateSST::isEnd() const {
    return status == END;
}
bool BreakPointStateSST::isInited() const {
    return status == INITED;
}
bool BreakPointStateSST::isConnected() const{
    return status == CONNECTED;
}
bool BreakPointStateSST::isLogined() const {
    return status == LOGINED;
}
bool BreakPointStateSST::isDisconnected() const {
    return status == DISCONNECTED;
}

// State Switch Table   
//      [cur][next] == 1 => switch Ok;  
//      [cur][next] == 0 -> Failed
//            END -> INITED -> CONNECTED -> LOGINED -> DISCONNECTED
//    
//END          0       1           0           0           0
//
//INITED       1       0           1           0           0
//
//CONNECTED    1       0           0           1           1
//
//LOGINED      1       0           0           0           1
//
//DISCONNECTED 1       0           0           0           0
int BreakPointStateSST::toEnd() {
    return toNext(END);
}
int BreakPointStateSST::toInited()  {
    return toNext(INITED);
}
int BreakPointStateSST::toConnected() {
    return toNext(CONNECTED);
}
int BreakPointStateSST::toLogined() {
    return toNext(LOGINED);
}
int BreakPointStateSST::toDisconnected() {
    return toNext(DISCONNECTED);
}

int BreakPointStateSST::toNext(int next){
    if((*sst)[status][next] == 1){
        status = next;
        return 0;
    }
    return -1;
}

std::ostream &  BreakPointStateSST::log(std::ostream & out){
    out << "\tstatus :"  << status ;
    IBreakPointState::log(out);
    out << std::endl;
    return out;
}
